package samsung.sds.system.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import samsung.sds.system.common.model.QueryParam;

/**
 * 通用工具类
 * 
 * @author Samsung
 * 
 */
public class CommonTools {
	/**
	 * 检测字符串是否不为空(null,"","null")
	 * 
	 * @param s
	 * @return 不为空则返回true，否则返回false
	 */
	public static boolean notEmpty(String s) {
		return s != null && !"".equals(s) && !"null".equals(s);
	}

	/**
	 * 检测字符串是否为空(null,"","null")
	 * 
	 * @param s
	 * @return 为空则返回true，不否则返回false
	 */
	public static boolean isEmpty(String s) {
		return s == null || "".equals(s) || "null".equals(s);
	}

	public static String getSessionUserName() {
		String username = "AnonymousOrOtherThing";
		try{
			UserDetails userDetails = (UserDetails) SecurityContextHolder
					.getContext().getAuthentication().getPrincipal();	
			username = userDetails.getUsername();
		}catch(Exception e){
			//eat it.
		}
		
		return username;
	}

	public static String getCurrentDateTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());
	}

	public static String getKeyUUID() {
		String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		return id;
	}

	public final static QueryParam getQueryParam(HttpServletRequest request) {
		// 获取每页显示多少条
		String showCount = request.getParameter("showCount");
		if (showCount == null)
			showCount = "20";
		// 获取当前第几页
		String currentPage = request.getParameter("currentPage");
		if (currentPage == null)
			currentPage = "1";
		// 实体封装查询参数
		QueryParam param = new QueryParam();
		param.setShowCount(Integer.parseInt(showCount));
		param.setCurrentPage(Integer.parseInt(currentPage));

		String queryFieldList = request
				.getParameter("query_condition_field_list");
		if (queryFieldList != null) {
			String[] queryFields = queryFieldList.split(",");
			Map<String, Object> queryCondition = new HashMap<String, Object>();
			String tempVal;
			for (String field : queryFields) {
				tempVal = request.getParameter(field);
				// 数据库中要对敏感字符加以修改，在此进行替换以“=”为敏感字符标记
				tempVal = escapeSQLParam(tempVal);
				queryCondition.put(field, tempVal);
			}
			param.setQueryCondition(queryCondition);
		}
		return param;
	}

	public final static String escapeSQLParam(String param) {
		if (param == null || param.length() < 1)
			return param;
		param = param.replaceAll("=", "==");
		param = param.replaceAll("%", "=%");
		param = param.replaceAll("_", "=_");
		return param;

	}

	public final static String escapeHTMLParam(String param) {
		if (param == null || param.length() < 1)
			return param;
		param = param.replaceAll("<", "&lt;");
		param = param.replaceAll(">", "&gt;");
		param = param.replaceAll("&", "&amp;");
		param = param.replaceAll("\"", "&quot;");
		param = param.replaceAll("'", "&#39;");
		param = param.replaceAll("×", "&times;");
		param = param.replaceAll("÷", "&divide;");
		return param;

	}

	public static String getLoginOrAnonymousUserName() {
		String userName = "";
		if (SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal() instanceof String) {
			userName = "anonymousUser";
		} else {
			UserDetails userDetails = (UserDetails) SecurityContextHolder
					.getContext().getAuthentication().getPrincipal();
			userName = userDetails.getUsername();
		}
		return userName;
	}

}
